<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
  <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  <title>返回一个常量的值</title>
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-base.css" />
<link media="all" rel="stylesheet" type="text/css" href="styles/03e73060321a0a848018724a6c83de7f-theme-medium.css" />

 </head>
 <body class="docs"><div class="navbar navbar-fixed-top">
  <div class="navbar-inner clearfix">
    <ul class="nav" style="width: 100%">
      <li style="float: left;"><a href="function.connection-status.html">« connection_status</a></li>
      <li style="float: right;"><a href="function.define.html">define »</a></li>
    </ul>
  </div>
</div>
<div id="breadcrumbs" class="clearfix">
  <ul class="breadcrumbs-container">
    <li><a href="index.html">PHP Manual</a></li>
    <li><a href="ref.misc.html">杂项 函数</a></li>
    <li>返回一个常量的值</li>
  </ul>
</div>
<div id="layout">
  <div id="layout-content"><div id="function.constant" class="refentry">
 <div class="refnamediv">
  <h1 class="refname">constant</h1>
  <p class="verinfo">(PHP 4 &gt;= 4.0.4, PHP 5, PHP 7, PHP 8)</p><p class="refpurpose"><span class="refname">constant</span> &mdash; <span class="dc-title">返回一个常量的值</span></p>

 </div>
 
 <div class="refsect1 description" id="refsect1-function.constant-description">
  <h3 class="title">说明</h3>
  <div class="methodsynopsis dc-description">
   <span class="methodname"><strong>constant</strong></span>(<span class="methodparam"><span class="type">string</span> <code class="parameter">$name</code></span>): <span class="type"><a href="language.types.declarations.html#language.types.declarations.mixed" class="type mixed">mixed</a></span></div>

  <p class="simpara">
   返回 <code class="parameter">name</code> 对应的常量的值。
  </p>
  <p class="simpara">
   当你不知道常量名，却需要获取常量的值时，<span class="function"><strong>constant()</strong></span> 就很有用了。也就是说，常量名储存在一个变量里，或者由函数返回时。
  </p>
  <p class="simpara">
   该函数也适用 <a href="language.oop5.constants.html" class="link">类常量</a>。
  </p>
 </div>


 <div class="refsect1 parameters" id="refsect1-function.constant-parameters">
  <h3 class="title">参数</h3>
  <p class="para">
   <dl>
    
     <dt>
<code class="parameter">name</code></dt>

     <dd>

      <p class="para">
          常量名。
      </p>
     </dd>

    
   </dl>

  </p>
 </div>


 <div class="refsect1 returnvalues" id="refsect1-function.constant-returnvalues">
  <h3 class="title">返回值</h3>
  <p class="para">
  返回常量的值。
  </p>
 </div>


 <div class="refsect1 errors" id="refsect1-function.constant-errors">
  <h3 class="title">错误／异常</h3>
  <p class="para">
   如果常量未定义，会抛出 <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 异常。
   在 PHP 8.0.0 之前，会产生 <strong><code>E_WARNING</code></strong> 级别的错误。
  </p>
 </div>


 <div class="refsect1 changelog" id="refsect1-function.constant-changelog">
  <h3 class="title">更新日志</h3>
  <table class="doctable informaltable">
   
    <thead>
     <tr>
      <th>版本</th>
      <th>说明</th>
     </tr>

    </thead>

    <tbody class="tbody">
     <tr>
      <td>8.0.0</td>
      <td>
       如果常量未定义，<span class="function"><strong>constant()</strong></span> 现在会抛出
       <span class="classname"><a href="class.error.html" class="classname">Error</a></span> 异常。以前会产生一个
       <strong><code>E_WARNING</code></strong> 级别的错误并返回 <strong><code>null</code></strong>。
      </td>
     </tr>

    </tbody>
   
  </table>

 </div>


 <div class="refsect1 examples" id="refsect1-function.constant-examples">
  <h3 class="title">范例</h3>
  <p class="para">
   <div class="example" id="example-3442">
    <p><strong>示例 #1 <span class="function"><strong>constant()</strong></span> 的例子</strong></p>
    <div class="example-contents">
<div class="phpcode"><code><span style="color: #000000">
<span style="color: #0000BB">&lt;?php<br /><br />define</span><span style="color: #007700">(</span><span style="color: #DD0000">"MAXSIZE"</span><span style="color: #007700">,&nbsp;</span><span style="color: #0000BB">100</span><span style="color: #007700">);<br /><br />echo&nbsp;</span><span style="color: #0000BB">MAXSIZE</span><span style="color: #007700">;<br />echo&nbsp;</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">"MAXSIZE"</span><span style="color: #007700">);&nbsp;</span><span style="color: #FF8000">//&nbsp;和上行一样<br /><br /><br /></span><span style="color: #007700">interface&nbsp;</span><span style="color: #0000BB">bar&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">test&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br />class&nbsp;</span><span style="color: #0000BB">foo&nbsp;</span><span style="color: #007700">{<br />&nbsp;&nbsp;&nbsp;&nbsp;const&nbsp;</span><span style="color: #0000BB">test&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'foobar!'</span><span style="color: #007700">;<br />}<br /><br /></span><span style="color: #0000BB">$const&nbsp;</span><span style="color: #007700">=&nbsp;</span><span style="color: #DD0000">'test'</span><span style="color: #007700">;<br /><br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'bar::'</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$const</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;string(7)&nbsp;"foobar!"<br /></span><span style="color: #0000BB">var_dump</span><span style="color: #007700">(</span><span style="color: #0000BB">constant</span><span style="color: #007700">(</span><span style="color: #DD0000">'foo::'</span><span style="color: #007700">.&nbsp;</span><span style="color: #0000BB">$const</span><span style="color: #007700">));&nbsp;</span><span style="color: #FF8000">//&nbsp;string(7)&nbsp;"foobar!"<br /><br /></span><span style="color: #0000BB">?&gt;</span>
</span>
</code></div>
    </div>

   </div>
  </p>
 </div>


 <div class="refsect1 seealso" id="refsect1-function.constant-seealso">
  <h3 class="title">参见</h3>
  <p class="para">
   <ul class="simplelist">
    <li class="member"><span class="function"><a href="function.define.html" class="function" rel="rdfs-seeAlso">define()</a> - 定义一个常量</span></li>
    <li class="member"><span class="function"><a href="function.defined.html" class="function" rel="rdfs-seeAlso">defined()</a> - 检查某个名称的常量是否存在</span></li>
    <li class="member"><span class="function"><a href="function.get-defined-constants.html" class="function" rel="rdfs-seeAlso">get_defined_constants()</a> - 返回所有常量的关联数组，键是常量名，值是常量值</span></li>
    <li class="member">关于 <a href="language.constants.html" class="link">常量</a> 的章节</li>
   </ul>
  </p>
 </div>


</div></div></div></body></html>